package com.ajax.example.controller;

import com.ajax.example.pojo.User;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@RestController  //这个注解表明,不经过视图解析器,即不进行页面跳转
public class AjaxController {

    @RequestMapping("/a1")
    public void ajax1(String name, HttpServletResponse resp) throws IOException {
        if("admin".equals(name)){
            resp.getWriter().println("true");
        }else{
            resp.getWriter().println("false");
        }
    }

    @RequestMapping("/a2")
    public List<User> ajax2(){
        ArrayList<User> list = new ArrayList<>();
        list.add(new User("Jack", 4, "male"));
        list.add(new User("Tom", 6, "female"));
        list.add(new User("Jerry", 1, "male"));
        return list;  //由于@RestController注解,将list转成json格式返回
    }

    @RequestMapping("/a3")
    public String ajax3(String name, String pwd){
       String msg="";
       //模拟数据库中存在数据
        if(name != null){
            if("admin".equals(name)){
                msg = "OK";
            }else{
                msg = "用户名输入错误";
            }
        }

        if(pwd != null){
            if("123456".equals(pwd)){
                msg = "OK";
            }else{
                msg = "密码输入错误";
            }
        }
        return msg;  //由于@RestController注解,将list转成json格式返回
    }


}
